﻿using AdminFrame.Localization;
using AdminFrame.MultiTenancy;
using AdminFrame.Permissions;
using System.Threading.Tasks;
using Volo.Abp.Identity.Web.Navigation;
using Volo.Abp.SettingManagement.Web.Navigation;
using Volo.Abp.TenantManagement.Web.Navigation;
using Volo.Abp.UI.Navigation;

namespace AdminFrame.Web.Menus;

public class AdminFrameMenuContributor : IMenuContributor
{
    public async Task ConfigureMenuAsync(MenuConfigurationContext context)
    {
        if (context.Menu.Name == StandardMenus.Main)
        {
            await ConfigureMainMenuAsync(context);
        }
    }

    private Task ConfigureMainMenuAsync(MenuConfigurationContext context)
    {
        var administration = context.Menu.GetAdministration();
        var l = context.GetLocalizer<AdminFrameResource>();

        context.Menu.Items.Insert(
            0,
            new ApplicationMenuItem(
                AdminFrameMenus.Home,
                l["Menu:Home"],
                "~/",
                icon: "fas fa-home",
                order: 0
            )
        );

        // 系统管理菜单 - 使用具体的访问权限
        var sysManagementMenu = new ApplicationMenuItem(
            AdminFrameMenus.SysManagement,
            l["SysManagement"],
            icon: "fa fa-cog",
            requiredPermissionName: AdminFramePermissions.SysManagementAccess,
            order: 1
        );

        // 系统管理-参数管理
        sysManagementMenu.AddItem(
            new ApplicationMenuItem(
                AdminFrameMenus.Parameters,
                l["Parameters"],
                url: "/Parameters",
                icon: "fa fa-list",
                requiredPermissionName: AdminFramePermissions.Parameters
            )
        );
        // 将系统管理菜单添加到主菜单
        context.Menu.AddItem(sysManagementMenu);

        // 配置管理菜单的顺序
        administration.SetSubItemOrder(IdentityMenuNames.GroupName, 2);
        administration.SetSubItemOrder(SettingManagementMenuNames.GroupName, 3);


        if (MultiTenancyConsts.IsEnabled)
        {
            administration.SetSubItemOrder(TenantManagementMenuNames.GroupName, 1);
        }
        else
        {
            administration.TryRemoveMenuItem(TenantManagementMenuNames.GroupName);
        }

        return Task.CompletedTask;
    }
}
